package com.li.shop.controller;

import com.li.shop.domain.Dictionary;
import com.li.shop.service.DictionaryService;
import com.li.shop.utils.SearchDictionaryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/Dictionary")
public class DictionaryController {
    @Autowired
    DictionaryService dictionaryService;

    @RequestMapping(value="dictionary_insert",method = RequestMethod.GET)
    public String dictionary_insert(Dictionary dictionary)
    {
        dictionaryService.add(dictionary);
        return "redirect:/Dictionary/dictionary_list";
    }
    @RequestMapping(value = "/dictionary_list",method = RequestMethod.GET)
    public  String list(Model model ,Integer pageSize,Integer pageNum){
        // List<Dictionary> dictionaries=dictionaryService.list();
        //model.addAttribute("dictionaries",dictionaries);
        return "back/dictionary-list";

    }
    @GetMapping("/api/searchdictionary")
    @ResponseBody//需要返回json数据
    public SearchDictionaryResponse searchUser(Integer pageNum, Integer pageSize , String keyword){
        return dictionaryService.listUser(pageSize,pageNum,keyword);
    }
    @GetMapping("dictionary_edit")
    public String dictionary_edit(@RequestParam("dId") String dId, Model model)
    {
        Dictionary dictionary=dictionaryService.get(dId);
        model.addAttribute("dId",dictionary);
        //return "back/2";
        return "back/EditPage";
    }
    @RequestMapping("dictionary_update")
    public String dictionary_update(HttpServletResponse response, HttpServletRequest request)
    {
        String idstr=request.getParameter("dId");
        Dictionary dictionary=dictionaryService.get(idstr);
        request.setAttribute("dic",dictionary);
        return null;
    }
    @RequestMapping(value="dictionary_delete",method = RequestMethod.GET)
    public String dictionary_delete(@RequestParam("dId") String dId,Model model)
    {

        int delete=dictionaryService.delete(dId);
        return "back/dictionary-list";
    }
    @RequestMapping(value="dictionary_add",method = RequestMethod.POST)
    public String dictionary_add(Dictionary dictionary, Model model) {
        dictionaryService.updatename(dictionary);
        //int updated=dictionaryService.saveDictionary(dictionary);
        return "redirect:/Dictionary/dictionary_list";

    }
    @RequestMapping(value = "imageedit",method = RequestMethod.POST)
    public String imageedit(Dictionary dictionary, @RequestParam("file") MultipartFile multipartFile, Model model) throws IOException {
        String filename = multipartFile.getOriginalFilename();
        String sname = filename.substring(filename.lastIndexOf("."));
        filename = UUID.randomUUID() + sname;
        String filepath = "E:/myFile/";
        multipartFile.transferTo(new File(filepath + filename));
        dictionary.setPath("/myFile/" + filename);
        //int updated = dictionaryService.saveDictionary(dictionary);
        dictionary.getdId();
        dictionaryService.updatepath(dictionary);
        return "redirect:/Dictionary/dictionary_edit?dId="+dictionary.getdId();
    }



//    @RequestMapping(value = "/dictionary_list",method = RequestMethod.GET)
//    public  String list(Model model ,Integer pageSize,Integer pageNum){
//        List<Dictionary> dictionaries=dictionaryService.list();
//        model.addAttribute("dic",dictionaries);
//        return "back/1";
//    }


}

